//
//  VSNode.m
//  ViewSeer
//
//  Created by LevinYan on 2017/3/22.
//  Copyright © 2017年 LevinYan. All rights reserved.
//

#import "VSNode.h"

@implementation VSNode

+ (instancetype)node:(UIView *)view
{
    return [[VSNode alloc] initWithNode:view];
}

- (instancetype)initWithNode:(nonnull UIView*)view
{
    self = [super init];
    
    _view = view;
    
    NSMutableArray<VSNode*> *subnodes = [NSMutableArray array];
    
    NSArray *subviews = self.view.subviews;
    [subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; //For render, every view only has one layer,no sublayers
    
    for(UIView *subview in subviews){
        
        if(![subview conformsToProtocol:@protocol(UILayoutSupport)])
            [subnodes addObject:[VSNode node:subview]];
    }

    _subnodes = [NSArray arrayWithArray:subnodes];
    return self;
}

- (NSArray<VSNode*> *)allSubnodes
{
    _allSubnodes = [NSMutableArray array];
    [self getSubnodes:(NSMutableArray*)_allSubnodes];
    return _allSubnodes;
}
- (void)getSubnodes:(NSMutableArray<VSNode*>*)nodes
{
    [nodes addObjectsFromArray:self.subnodes];
    for(VSNode* subnode in self.subnodes){
        
        if(subnode.subnodes.count){
        
            [subnode getSubnodes:nodes];
        }
    }
}
- (NSString*)name
{
    return NSStringFromClass([self.view class]);
}

@end
